/*
 * KeyWordIndex.cpp
 *
 *  Created on: Mar 7, 2011
 *      Author: Craig Wilson
 */

#include"../inc/KeyWordIndex.h"

		ostream &operator<<(ostream &stream, MapNode *o){
  			stream << o->getWord();
  			return stream;
		}


		ostream &operator<<(ostream &stream, MapNode o){
 			 stream << *(o.getWord());
  			return stream;
		}

		KeyWordIndex::KeyWordIndex(){

		}

		void KeyWordIndex::add(string &word, Page& site){
			MapNode *node;
			MapNode temp(word);
			if (tree.contains(temp)){
				node =tree.FindItem(temp);
				node->add(site);
			}
			else{
				node= new MapNode(word);
				node->add(site);
				tree.Insert(*node);
				delete node; //Tree makes a copy of the node.
			}
		}

		OccurrenceSet* KeyWordIndex::getSet(string &word){
			MapNode temp(word);
			OccurrenceSet *found = &(tree.FindItem(temp)->set);
			return found;
		}

		LinkedList<string>* KeyWordIndex::getWords(){
			LinkedList<MapNode> *Occurrences=tree.toLinkedList();

			LinkedList<string> *words= new LinkedList<string>();

			for(int i=0;i<Occurrences->GetSize();i++){
				words->Insert(*(Occurrences->Get(i)->word));
			}
			delete Occurrences;
			return words;
		}
